
"use strict";

angular.module('app.user').controller('AllUserCtrl', function ($scope,$cookieStore,$http) {
    //配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [1, 2, 3, 40, 50],
        onChange: function () {
        }
    };
    // $scope.userid = $cookieStore.get('userid');
    $scope.searchid = '';
	$scope.get_user = function(){
		var page_obj = {
            sidx:'z_id',
            sord:'desc',
			page:$scope.paginationConf.currentPage,
            rows:$scope.paginationConf.itemsPerPage
		}
		$http.post(baseurl+'/list/select/view_role_user?id='+$scope.searchid,page_obj).then(function(res){
            // console.log(res.data);
			if (res.data.ret == 1) {
				$scope.datas = res.data.rows;
				// console.log(typeof $scope.datas[0].roleid)
			    $scope.paginationConf.totalItems = res.data.records;
			}			
		})
	}
	$scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.get_user);
	// 添加
    $scope.addbox = function(){
        $scope.addobj = {};
        $scope.addobj.roleid = '0';
        $scope.addobj.warn_grade = '-1';
        $("#addAlert").modal("show");
    }
    $scope.sure_add = function(){
        // var len = Object.keys($scope.addobj).length;
    	if($scope.addobj.id == null || $scope.addobj.id == ''){
    		new Notify('fail','账号不能为空'); 
    		return;
    	}
        if($scope.addobj.tele != null && $scope.addobj.tele != ''){
            if(checkMobile($scope.addobj.tele) == false){
               new Notify('info','手机号码输入错误'); 
               return;
            }           
        }
    	$http.post(baseurl+'/data/user',$scope.addobj).then(function(res){
            console.log(res.data);
            if (res.data.ret == 1) {
            	 $("#addAlert").modal("hide");
            	 $scope.get_user();
            }else if(res.data.ret != -4){
				new Notify('fail','账号已注册'); 	
            }
    	});
    }
    // 手机号验证
    function checkMobile(sMobile){ 
        if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(sMobile))){ 
            return false; 
        }else{
            return true;
        }
    } 
	// 修改
	$scope.showEditAlert = function(item){
		$scope.editid = item.id;
		$scope.editobj = {
            id:item.id,
            name:item.name,
            wx_openid:item.wx_openid,
            roleid:item.roleid,
            warn_grade:item.warn_grade,
            tele:item.tele
        };
	}
    $scope.sure_edit = function(){
        if($scope.editobj.tele != null && $scope.editobj.tele != ''){
            if(checkMobile($scope.editobj.tele) == false){
               new Notify('info','手机号码输入错误'); 
               return;
            }           
        }
    	$http.put(baseurl+'/data/user?id=='+$scope.editid,$scope.editobj).then(function(res){
            // console.log(res.data);
            if (res.data.ret == 1) {
            	 $("#editAlert").modal("hide");
                 new Notify('success','修改成功');
            	 $scope.get_user();
            }
    	});
    }
	// 删除
	$scope.showDelDialog = function(id){
		$scope.delid = id;
	}
	$scope.sure_del = function(){
    	$http.delete(baseurl+'/data/user?id=='+$scope.delid).then(function(res){
            // console.log(res.data);
            if (res.data.ret == 1) {
            	 $("#delAlert").modal("hide");
            	 $scope.get_user();
            }
    	});
	}
	//获取角色列表
	$scope.getrole_list = function(){
		$http.get(baseurl+'/select_idname/role').then(function(res){
			// console.log(res.data);
			if (res.data.ret == 1) {
                $scope.roles = res.data.info;
			}		
		})
	}
	$scope.getrole_list();
    // 用户修改角色
    $scope.modify_role = function(id){
      // console.log(id);
      // team_box(id);
    }
    // 角色弹出框
    var team_box = function(id){
        // event.stopPropagation();
        $(".sele_team").css({
            width: "100px"
        })
        $(".header_box_role"+id).fadeToggle();
        $(".header_box").hide();
        $(".header_box_role"+id).css({
            top: "-50px",
            left: "-30px",
            width: "215px",
            zIndex: '1000'
        })
    }
});